package task

import (
	"net/http"

	"gitea.in/wx-rdc/service-base/internal/code"
	"gitea.in/wx-rdc/service-base/internal/pkg/core"
	"gitea.in/wx-rdc/service-base/internal/services/task"
)

type listRequest struct {
	Page       int     `json:"page" binding:"required"`
	PageSize   int     `json:"page_size" binding:"required"`
	Title      *string `json:"title"`       // 任务名称
	Status     *int32  `json:"status"`      // 任务状态
	CategoryId *int32  `json:"category_id"` // 任务分类
	UserId     *int32  `json:"user_id"`     // 任务负责人
	DueAt      *int64  `json:"due_at"`      // 任务截止时间
}

type listResponse struct {
	Total int64            `json:"total"`
	List  []*task.TaskItem `json:"list"`
}

// List 查询任务列表
// @Summary 查询任务列表
// @Tags 任务管理接口
// @Produce json
// @Param listRequest body listRequest false "查询参数"
// @Success 200 {object} object
// @Failure 400 {object} code.Failure
// @Router /task/list [post]
// @Security LoginToken
func (h *handler) List() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(listRequest)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}

		searchData := &task.SearchData{
			Page:       req.Page,
			PageSize:   req.PageSize,
			Title:      req.Title,
			Status:     req.Status,
			CategoryId: req.CategoryId,
			UserId:     req.UserId,
			DueAt:      req.DueAt,
		}
		total, list, err := h.taskService.List(ctx, searchData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.TaskError,
				code.Text(code.TaskError)).WithError(err),
			)
			return
		}

		res := &listResponse{
			Total: total,
			List:  list,
		}

		ctx.Payload(res)
	}
}
